package com.xianyun.site.support;

import com.alibaba.cola.dto.Response;
import com.alibaba.fastjson.JSONObject;
import com.xianyun.site.support.api.SmtpServiceI;
import com.xianyun.site.support.dto.ReceiveCmd;
import com.xianyun.site.support.dto.SmtpReceiveCmd;
import com.xianyun.site.support.dto.clientobject.CheckParamCO;
import com.xianyun.site.support.executor.commond.SmtpReceiveCmdExo;
import com.xianyun.site.support.executor.handle.smtp.v20201108.config.SmtpSendType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author mac_zyj
 */
@Slf4j
@Service
public class SmtpServiceImpl implements SmtpServiceI {


    @Autowired
    private SmtpReceiveCmdExo smtpReceiveCmdExo;

    @Override
    public Response analyseJsonAndSend(ReceiveCmd receiveCmd, JSONObject receiveObject) {
        CheckParamCO checkParam = receiveCmd.getCheckParam();
        SmtpReceiveCmd smtpReceiveCmd = receiveObject.toJavaObject(SmtpReceiveCmd.class);
        smtpReceiveCmd.setCheckParam(receiveCmd.getCheckParam());
        smtpReceiveCmd.getSmtpMessage().setConditionName(checkParam.getModelFunction()+ SmtpSendType.getSendTypeNameById(checkParam.getUserAction()));
        sendSingleApiMail(smtpReceiveCmd);
        return Response.buildSuccess();
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void sendSingleApiMail(SmtpReceiveCmd smtpReceiveCmd) {
        smtpReceiveCmdExo.execute(smtpReceiveCmd);
    }

    @Override
    public void sendBatchApiMail(SmtpReceiveCmd smtpParams) {

    }

}
